package cn.gson.springboot.controllers;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.springboot.model.pojos.DrugTypesYk;
import cn.gson.springboot.model.pojos.DrugsYk;
import cn.gson.springboot.model.pojos.FactoryYk;
import cn.gson.springboot.model.service.DrugsYkService;
import cn.gson.springboot.model.service.FactoryYkService;
import cn.gson.springboot.model.service.InventoryYkService;
import cn.gson.springboot.model.vo.InventoryYkVO;
import cn.gson.springboot.model.vo.ReturnDrugsYkVO;
import cn.gson.springboot.model.vo.RukuVo;

import org.springframework.ui.Model;

@Controller
public class DrugsController {
	@Autowired
	private DrugsYkService dyks;
	@Autowired
	private FactoryYkService fase;
	@Autowired
	private InventoryYkService ivs;

	//显示药品界面
	@RequestMapping("yaoku-yaopin")
	public void yaokuyaopin(Model mode) {
	}
	//查询所有药品信息
	@RequestMapping("yaopinbiaoge")
	@ResponseBody
	public Map<String,Object> hosLikeTableSmt(
			@RequestParam("page")int page,
			@RequestParam("rows")int rows){
		//进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray findall=dyks.findAll();
		
		//System.out.println("药品--"+findall);
		//进行数据格式转换和封装
		Map<String,Object> map = new HashMap<>();
		map.put("total", pg.getTotal());//封装总记录数
		map.put("rows", findall);//封装当前页的内容
		
		return map; 
	}
	
	//查所有药品
	@RequestMapping("yaopinxinxi")
	@ResponseBody
	public Map<String,Object> hosLikeTableSmt2(
			@RequestParam("page")int page,
			@RequestParam("rows")int rows){
		//进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray findall=dyks.findyaopin();
		
		//System.out.println("药品--"+findall);
		//进行数据格式转换和封装
		Map<String,Object> map = new HashMap<>();
		map.put("total", pg.getTotal());//封装总记录数
		map.put("rows", findall);//封装当前页的内容
		
		return map; 
	}
	//药品界面的药品类型下拉列表
	@RequestMapping("leixing")
	@ResponseBody
	public List<DrugTypesYk> leixinga() {
		return dyks.findlxing();
	}
	
	//根据供应商查询药品
	@PostMapping("gyscx")
	@ResponseBody
	public JSONArray findbyidgys(Integer factoryId){
			JSONArray findbyidgys =dyks.findbyidgys(factoryId);
			System.out.println(findbyidgys);
			return findbyidgys;
	};
	
	//药品新增
	@PostMapping("ypxz") 
	@ResponseBody
	public String zhuyuanxinzeng(@RequestParam("ypname")String ypname,
			@RequestParam("yguige")String yguige,
			@RequestParam("yjchuiage")BigDecimal yjchuiage,Integer factory_id,Integer drug_types_id,@RequestParam("ypbzq")String ypbzq) {	
		//查询已有的药品
		JSONArray mcgggys = dyks.mcgggys(ypname, yguige, factory_id);
		//判断如果有这个药品，没有就新增
		if(mcgggys.size()>0) {
			return "1";
		}else {
			//药品
			DrugsYk ss=new DrugsYk();
			ss.setDrugName(ypname);
			ss.setDrugSpecifications(yguige);
			ss.setDrugSellingPrice(yjchuiage);
			ss.setDrugPeriod(ypbzq);
			//供应商
			FactoryYk fa = new FactoryYk();
			FactoryYk ff=fase.findbyid(factory_id);
			ss.setFactoryYk(ff);
			//类型
			DrugTypesYk types = new DrugTypesYk();
			DrugTypesYk tt = dyks.bylxid(drug_types_id);
			ss.setDrugTypesYk(tt);

			dyks.xinzeng(ss);
			
			return "ypall";
		}	
	}
	
	//采购入库的药品查询所有
	@RequestMapping("suoyou")
	@ResponseBody
	public JSONArray drugList() {
		return dyks.drugList();
	}
	
	//采购入库的新增
	@RequestMapping("save-order")
	@ResponseBody
	public String saveOrder(RukuVo purVo) {
		try {
			dyks.saveOrder(purVo);
			return "新增成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "新增失败";
		}
	}
	
	//批次的新增 库存
	@RequestMapping("xinpici")
	@ResponseBody
	public String saveOrdera(InventoryYkVO purvo) {
		try {
			dyks.saveOrdera(purvo);
			return "新增成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "新增失败";
		}
	}
	
}

